package com.base.vistter.iframe.security.service;

import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.menu.ErrorLoginEnum;
import com.base.vistter.iframe.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Slf4j
public class DBUserDetailsService implements UserDetailsService, UserDetailsPasswordService {

    @Resource(name = "sysUserService")
    private SysUserService sysUserService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            SysUser sysUser = sysUserService.getSysUserByUsername(username);
            if (sysUser == null){
                throw new UsernameNotFoundException(ErrorLoginEnum.USERNAME_OR_PASSPWRD_ERROR.getErrorMessage());
            }
            return sysUser;
        } catch (IframeException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

}
